package com.happyghost.middleware.server.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RedPacketUtil {

    public static List<Integer> divideRadPackage(Integer totalAmount, Integer totalPeopleNum) {
        List<Integer> amountList = new ArrayList<>();

        if (totalAmount > 0 && totalPeopleNum > 0) {
            Integer restAmount = totalAmount;
            Integer restPeopleNum = totalPeopleNum;
            Random random = new Random();
            for (int i = 0; i < totalPeopleNum - 1; i++) {
                int amount = random.nextInt(restAmount / restPeopleNum * 2 - 1) + 1;
                restAmount -= amount;
                restPeopleNum--;
                amountList.add(amount);
            }

            amountList.add(restAmount);
        }
        return amountList;
    }

    public static void main(String[] args) {
        List<Integer> integers = divideRadPackage(10, 3);

        integers.forEach(item -> {
            System.out.println(item);
        });
    }
}
